<?php
/**
 * 基础Model类
 * @author:fanjunjun
 * @date：2017 11 17
 */
namespace app\index\model;
use think\Model;

class Base extends Model {

    /**
     * Add one item
     * @param data array(id,room_item_id,type,user_id,ctime,mtime,status)
     * @return id or false
     * @when return false, status -1 for params error, -2 for repeat, -3 for add exception
     */
    public function addData($data, &$status) {
        $status = 0;
        if (!is_array($data) || $data == false) {
            $status = -1;
            return false;
        } else if (count($data) == 0) { // check not empty data
            $status = -1;
            return false;
        }
        $obj = $this->where($data)->find();
        if (!$obj) {
            // If has ctime, set ctime to now
            if (true && !isset($data['ctime'])) {
                $data['ctime'] = time();
            }
            return $this->save($data);
        } else {
            $status = -2;
            return $obj['id'];
        }

        $status = -3;
        return false;
    }

    /**
     * Delete by field
     * @param field: live.room_item_action_log key => value
     * @return true or false
     */
    public function delDataByField($field) {
        if (!is_array($field) || $field == false) {
            return false;
        } else {
            return $this->where($field)->delete();
        }
    }

    /**
     * Update live.room_item_action_log info
     * @param data: array(id,room_item_id,type,user_id,ctime,mtime,status)
     * @return true or false
     */
    public function updateData($data) {
        $table_pk = $this->getPk();

        if (!isset($data["{$table_pk}"])) {
            return false;
        }

        $where = array("{$table_pk}" => $data["{$table_pk}"]);
        unset($data["{$table_pk}"]);

        if (0 == $this->where($where)->count()) {
            return false;
        }

        // If table has mtime, set mtime to now
        if (true && !isset($data['mtime'])) {
            $data['mtime'] = time();
        }

        return $this->where($where)->save($data);
    }

    /**
     * Query by sql
     * @param sql
     * @return array, count of array is 0 for the result is empty
     **/
    public function getDataBySql($sql) {
        $objs = $this->query($sql);

        $data = array();
        foreach ($objs as $obj) {
            $data[] = $this->_parseData($obj);
        }

        return $data;
    }

    /**
     * Query by sql
     * @param sql
     * @return count
     **/
    public function getCountBySql($sql) {
        $data = $this->query($sql);

        return intval($data[0]['count']);
    }

    /**
     * Get count by where
     * @param where
     * @return count
     */
    public function getCountByField($where) {
        return intval($this->where($where)->count());
    }

    /**
     * Execute a sql
     * @param sql
     * @return true/false
     */
    public function executeSql($sql) {
        $res = $this->execute($sql);
        if ($res === false) {
            return $res;
        }
        return true;
    }

    /**
     * Query by pk
     * @param id
     * @return false or array
     **/
    public function getDataByPk($id) {
        if ($id == 0) {
            return false;
        }
        $item = $this->find($id);
        return $this->_parseData($item);
    }

    /**
     * Get info by some fields
     * @param field: array of live.room_item_action_log key => value
     * @return array of live.room_item_action_log info
     */
    public function getDataByField($field) {
        if (count($field) <= 0 || !is_array($field)) {
            return array();
        }
        $objs = $this->where($field)->select();
        return $objs;
    }

    /**
     * Get info by ids
     * @param ids: live.room_item_action_log ids
     * @return array of live.room_item_action_log info
     */
    public function getDataByIds($ids) {
        if (!is_array($ids) || $ids == false) {
            return array();
        }
        $objs = $this->where(array('id' => array('IN', $ids)))->findAll();

        $data = array();
        foreach ($objs as $obj) {
            $data[] = $this->_parseData($obj);
        }

        return $data;
    }

    /**
     * Get all live.room_item_action_log info
     * @return all live.room_item_action_log info
     */
    public function getAllData() {
        $objs = $this->findAll();

        $data = array();
        foreach ($objs as $obj) {
            $data[] = $this->_parseData($obj);
        }

        return $data;
    }

    /**
     * Get live.room_item_action_log info by page
     * @field live.room_item_action_log key => value
     * @page GT 0
     * @num GT 0
     * @order sort condition
     * @return array of live.room_item_action_log info
     */
    public function getPageDataByField($field, $page=1, $num=10, $order="") {
        if (!is_array($field) || $field == false) {
            return array();
        }

        if (strlen($order) > 0) {
            $objs = $this->where($field)->order($order)->limit($num)->page($page)->findAll();
        } else {
            $objs = $this->where($field)->limit($num)->page($page)->findAll();
        }

        $data = array();
        foreach ($objs as $obj) {
            $data[] = $this->_parseData($obj);
        }

        return $data;
    }

    /**
     * Parse live.room_item_action_log info
     * This function only parse table's core infomation
     * @return live.room_item_action_log info
     */
    private function _parseData($data) {
        return $data;
    }

    /**
     * Format live.room_item_action_log info
     * This function format table's extension infomation
     * @return live.room_item_action_log info
     */
    public function formatData($data) {
        return $data;
    }

    /**
     * change some statistics field value
     * @key privage key
     * @field need changed field
     * @type inc or dec or reset
     * @return boolean
     */
    public function changeFieldNumByKey($key, $field, $type) {
        if (!is_array($key) || strlen($field) <= 0 || !in_array($type, array('INC', 'DEC', 'RESET'))) {
            return false;
        }

        if ($this->where($key)->count() != 1) {
            return false;
        }

        if ($type == 'INC') {
            return $this->where($key)->setInc($field);
        } else if ($type == 'DEC') {
            return $this->where($key)->setDec($field);
        } else {
            return $this->where($key)->save(array($field => 0));
        }

        return false;
    }
}
